import {
  getPersonalInfoFull,
  updateUserSubject,
  updateUserPhase,
  updateUserArea,
  updateUserFocus,
  updateUserGoodAt,
  updateUserTrainSpeciality
} from '../../../api/user'
import { areaList, findColumnList, listByKind } from '../../../api/base'

Page({
  data: {
    loading: true,
    form: {},
    avatarUrl: '',
    sex: '',

    subjectList: [],
    showSubject: false,
    selSubjects: [],

    phaseList: [],
    showPhase: false,
    selPhases: '',

    goodAtList: [],
    showGoodAt: false,
    selGoodAt: [],

    focusList: [],
    showFocus: false,
    selFocus: [],

    trainList: [],
    showTrain: false,
    selTrain: []
  },
  onLoad: function (options) {
    wx.showLoading()
    listByKind('stage').then(res => {
      if (res.length) {
        res.forEach(r => {
          r.isCheck = false
        })
      }
      this.setData({
        phaseList: res
      })
    })
    // 科目
    listByKind('subject').then(res => {
      if (res.length) {
        res.forEach(r => {
          r.isCheck = false
        })
      }
      this.setData({
        subjectList: res
      })
      this.init()
    })
  },
  init() {
    const _this = this
    getPersonalInfoFull().then(res => {
      // 性别参数转换
      const genders = {
        MALE: '男', FEMALE: '女', SECRET: '保密'
      }
      // 科目选项转换
      let selSubject = []
      let subjectList = this.data.subjectList
      if (res.subjects.length) {
        res.subjects.forEach(sub => {
          selSubject.push({
            code: sub.subjectId,
            name: sub.subjectName
          })
        })
        res.subjects.forEach(sub => {
          subjectList.forEach(list => {
            if (sub.subjectId === list.code) {
              list.isCheck = true
            }
          })
        })
      }
      // 学段选项转换
      let selPhases = []
      let phaseList = this.data.phaseList
      if (res.phase.length) {
        res.phase.forEach(pha => {
          selPhases.push({
            code: pha.itemCode,
            name: pha.itemName
          })
        })
        res.phase.forEach(pha => {
          phaseList.forEach(list => {
            if (pha.itemCode === list.code) {
              list.isCheck = true
            }
          })
        })
      }

      this.setData({
        form: res,
        avatarUrl: res.avatarUrl,
        sex: genders[res.sex],
        selSubjects: selSubject,
        subjectList: subjectList,
        selPhases: selPhases,
        phaseList: phaseList,
        areaCode: res.area.districtCode,
        loading: false
      })
      this.getFindColumnList();

      wx.setStorageSync('userAvatar', res.avatarUrl)
      wx.setStorageSync('userNickName', res.name)
      wx.hideLoading()
    })
  },
  changeAvatar() {
    const _this = this
    wx.navigateTo({
      url: '/packageA/pages/changeAvatar/avatar?key=' + this.data.form.avatarUrl
    })
  },
  changeName() {
    wx.navigateTo({
      url: '/packageA/pages/changeName/index?key=' + this.data.form.name
    })
  },
  changeSex() {
    const _this = this
    wx.navigateTo({
      url: '/packageA/pages/changeGender/selectGender?key=' + this.data.form.sex
    })
  },
  changeIdentityType() {
    wx.navigateTo({
      url: '/packageA/pages/changeIdentityType/identityType?key=' + this.data.form.identityType
    })
  },
  changeSchool() {
    getApp().globalData.area = this.data.form.area
    wx.navigateTo({
      url: '/packageA/pages/changeSchool/index?key=' + this.data.form.school
    })
  },
  changePhone() {
    wx.navigateTo({
      url: '/packageA/pages/changePhone/index?key=' + this.data.form.mobileNumber
    })
  },
  changeEmail() {
    wx.navigateTo({
      url: '/packageA/pages/changeEmail/index?key=' + this.data.form.email
    })
  },

  /** 选择科目 start **/
  selectSubject() {
    this.setData({showSubject: true})
  },

  cancelSelectSubject() {
    this.setData({showSubject: false})
  },

  checkSubject(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.subjectList[index].isCheck = item.isCheck
    this.setData({
      subjectList: this.data.subjectList
    })
  },

  confirmSelectSubject() {
    const _this = this
    _this.data.selSubjects = []
    _this.data.subjectList.forEach(subject => {
      if (subject.isCheck) {
        _this.data.selSubjects.push(subject)
      }
    })
    let selSubjects = [];
    if (_this.data.selSubjects.length) {
      _this.data.selSubjects.forEach(sub => {
        selSubjects.push(sub.code)
      })
    }
    updateUserSubject(selSubjects).then(res => {
      _this.setData({
        showSubject: false,
        selSubjects: this.data.selSubjects,
        goodAtList: [],
        focusList: [],
        trainList: []
      })
      _this.getFindColumnList()
    })
  },
  /** 选择科目 end **/

  /** 选择学段 start **/
  selectPhase() {
    this.setData({showPhase: true})
  },

  cancelSelectPhase() {
    this.setData({showPhase: false})
  },

  checkPhase(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.phaseList[index].isCheck = item.isCheck
    this.setData({
      phaseList: this.data.phaseList
    })
  },

  confirmSelectPhase() {
    const _this = this
    _this.data.selPhases = []
    _this.data.phaseList.forEach(phase => {
      if (phase.isCheck) {
        _this.data.selPhases.push(phase)
      }
    })
    let selPhases = [];
    if (_this.data.selPhases.length) {
      _this.data.selPhases.forEach(pha => {
        selPhases.push(pha.code)
      })
    }
    updateUserPhase(selPhases).then(res => {
      _this.setData({
        showPhase: false,
        selPhases: this.data.selPhases,
        goodAtList: [],
        focusList: [],
        trainList: []
      })
      _this.getFindColumnList()
    })
  },
  /** 选择学段 end **/

  changeTeacherCert() {
    const _this = this
    getApp().globalData.teacherCert = this.data.form.teacherCert
    wx.navigateTo({
      url: '/packageA/pages/changeTeacherCert/index'
    })
  },

  changeStudentCert() {
    getApp().globalData.studentCert = this.data.form.studentCert
    wx.navigateTo({
      url: '/packageA/pages/changeStudentCert/index'
    })
  },

  changeIdCartCert() {
    getApp().globalData.idCard = this.data.form.idCard
    wx.navigateTo({
      url: '/packageA/pages/changeIdCartCert/index'
    })
  },

  changeZyzCert() {
    getApp().globalData.zyz = this.data.form.zyz
    wx.navigateTo({
      url: '/packageA/pages/changeZyzCert/index'
    })
  },

  getFindColumnList() {
    if (!this.data.goodAtList.length || !this.data.focusList.length || !this.data.trainList.length) {
      let selPhases = [];
      if (this.data.selPhases.length) {
        this.data.selPhases.forEach(pha => {
          selPhases.push(pha.code)
        })
      }
      let selSubjects = [];
      if (this.data.selSubjects.length) {
        this.data.selSubjects.forEach(sub => {
          selSubjects.push(sub.code)
        })
      }
      findColumnList({
        stageList: selPhases,
        subjectList: selSubjects
      }).then(res => {
        res.forEach(tag => {
          tag.isCheck = false;
          if (tag.sub && tag.sub.length) {
            tag.sub.forEach(s => {
              s.isCheck = false
            })
          }
        })
        this.setData({
          goodAtList: JSON.parse(JSON.stringify(res)),
          focusList: JSON.parse(JSON.stringify(res)),
          trainList: JSON.parse(JSON.stringify(res))
        })

        // 擅长选项转换
        let selGoodAt = []
        let goodAtList = this.data.goodAtList
        if (this.data.form.goodAtLabels.length) {
          this.data.form.goodAtLabels.forEach(item => {
            selGoodAt.push(item)
          })
          this.data.form.goodAtLabels.forEach(item => {
            goodAtList.forEach(list => {
              if (item.tagsId === list.tagsId) {
                list.isCheck = true
              }
            })
          })
          this.setData({
            selGoodAt: selGoodAt,
            goodAtList: goodAtList
          })
        }
        // 关注选项转换
        let selFocus = []
        let focusList = this.data.focusList
        if (this.data.form.focusLabels.length) {
          this.data.form.focusLabels.forEach(item => {
            selFocus.push(item)
          })
          this.data.form.focusLabels.forEach(item => {
            focusList.forEach(list => {
              if (item.tagsId === list.tagsId) {
                list.isCheck = true
              }
            })
          })
          this.setData({
            selFocus: selFocus,
            focusList: focusList
          })
        }

        // 培训选项转换
        let selTrain = []
        let trainList = this.data.trainList
        if (this.data.form.trainSpecialityLabels.length) {
          this.data.form.trainSpecialityLabels.forEach(item => {
            selTrain.push(item)
          })
          this.data.form.trainSpecialityLabels.forEach(item => {
            trainList.forEach(list => {
              if (item.tagsId === list.tagsId) {
                list.isCheck = true
              }
            })
          })
          this.setData({
            selTrain: selTrain,
            trainList: trainList
          })
        }
      })
    }
  },

  /** 选择擅长内容 start **/
  selectGoodAt() {
    this.setData({showGoodAt: true})
  },

  cancelSelectGoodAt() {
    this.setData({showGoodAt: false})
  },

  checkGoodAt(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.goodAtList[index].isCheck = item.isCheck
    this.setData({
      goodAtList: this.data.goodAtList
    })
  },

  confirmSelectGoodAt() {
    this.data.selGoodAt = []
    this.data.goodAtList.forEach(goodAt => {
      if (goodAt.isCheck) {
        this.data.selGoodAt.push(goodAt)
      }
    })

    let selGoodAt = [];
    if (this.data.selGoodAt.length) {
      this.data.selGoodAt.forEach(tag => {
        selGoodAt.push(tag.tagsId)
      })
    }
    updateUserGoodAt(selGoodAt).then(res => {
      this.setData({
        showGoodAt: false,
        selGoodAt: this.data.selGoodAt
      })
    })
  },
  /** 选择擅长内容 end **/

  /** 选择关注内容 start **/
  selectFocus() {
    this.setData({showFocus: true})
  },

  cancelSelectFocus() {
    this.setData({showFocus: false})
  },

  checkFocus(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.focusList[index].isCheck = item.isCheck
    this.setData({
      focusList: this.data.focusList
    })
  },

  confirmSelectFocus() {
    this.data.selFocus = []
    this.data.focusList.forEach(focus => {
      if (focus.isCheck) {
        this.data.selFocus.push(focus)
      }
    })

    let selFocus = [];
    if (this.data.selFocus.length) {
      this.data.selFocus.forEach(tag => {
        selFocus.push(tag.tagsId)
      })
    }
    updateUserFocus(selFocus).then(res => {
      this.setData({
        showFocus: false,
        selFocus: this.data.selFocus
      })
    })
  },
  /** 选择关注内容 end **/

  /** 选择培训专长 start **/
  selectTrain() {
    this.setData({showTrain: true})
  },

  cancelSelectTrain() {
    this.setData({showTrain: false})
  },

  checkTrain(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.isCheck = !item.isCheck
    this.data.trainList[index].isCheck = item.isCheck
    this.setData({
      trainList: this.data.trainList
    })
  },

  confirmSelectTrain() {
    this.data.selTrain = []
    this.data.trainList.forEach(train => {
      if (train.isCheck) {
        this.data.selTrain.push(train)
      }
    })

    let selTrain = [];
    if (this.data.selTrain.length) {
      this.data.selTrain.forEach(tag => {
        selTrain.push(tag.tagsId)
      })
    }
    updateUserTrainSpeciality(selTrain).then(res => {
      this.setData({
        showTrain: false,
        selTrain: this.data.selTrain
      })
    })
  },
  /** 选择培训专长 end **/
})
